package com.example.demomybatis.controller;


import com.example.demomybatis.entity.Course;
import com.example.demomybatis.service.CourseService;
import com.example.demomybatis.vo.RequestCoursePr;
import com.example.demomybatis.vo.ResultCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class TestController {

    @Autowired
    CourseService courseService;


    @RequestMapping("/addCourse")
    public String addCourse(Course course){

        courseService.addCourse(course);

        return "redirect:/toCourseList";
    }

    @RequestMapping("/toEditCourse")
    public String toEditCourse(Model model,Integer id){
        Course course  = courseService.selectOneCourse(id);
        model.addAttribute("course",course);
        return  "course/updateCourse";
    }

    @RequestMapping("/toAddCourse")
    public String toAddCourse(){
        return "course/addCourse";
    }


//    @RequestMapping("/toCourseList")
//    public String toCourseList(Model model){
//        List<Course> courseList = courseService.selectCourse();
//        System.out.println(courseList.toString());
//        model.addAttribute("courseList",courseList);
//       return "course/courseList";
//    }

    @RequestMapping("/deleteCourse")
    public String deleteCourse(Integer id){

        String  result = "";
        try {
            Integer status = courseService.deleteCourse(id);
            if(status>0)
                result="删除成功！删除数据ID是: "+id;
        }catch (Exception e){
            e.printStackTrace();
            result="删除失败！异常是: "+e.getMessage();
        }
        System.out.println(result);
        return "redirect:/toCourseList";
    }

    @RequestMapping("/selectCourse")
    @ResponseBody
    public ResultCourseVo selectCourse(@RequestBody RequestCoursePr requestCoursePr){
        ResultCourseVo resultVo  = new ResultCourseVo();
        System.out.println("requestCoursePr.getPageNum()  ：  "+requestCoursePr.toString());
//        if(requestCoursePr.getPageNum()>1){
            requestCoursePr.setPageNum((requestCoursePr.getPageNum()-1)*requestCoursePr.getPageSize());
//        }
//        else{
//            requestCoursePr.setPageNum(0);
//        }
        resultVo.setList(courseService.selectCourse(requestCoursePr));
        resultVo.setTotal(courseService.selectCountCourse(requestCoursePr));
        return resultVo;
    }


    @RequestMapping("/getOneCourse")
    @ResponseBody
    public Course getOneCourse(Integer courseId){

        return courseService.selectOneCourse(courseId);
    }


    @RequestMapping("/deleteCourseById")
    @ResponseBody
    public Integer deleteCourseById(Integer courseId){
        return courseService.deleteCourse(courseId);
    }


    @RequestMapping("/updateCourseById")
    @ResponseBody
    public Integer updateCourseById(Course course){
        return courseService.updateCourse(course);
    }

    @RequestMapping("/addCourseBy")
    @ResponseBody
    public Course addCourseBy(Course course){
        return courseService.addCourse(course);
    }

    @RequestMapping("/updateCourse")
    public String updateCourse(Course course){

        String  result = "";
        try {
            Integer status = courseService.updateCourse(course);
            if(status>0)
                result="更新成功！添加数据是: "+course.toString();
        }catch (Exception e){
            e.printStackTrace();
            result="更新失败！异常是: "+e.getMessage();
        }
        System.out.println(result);
        return "redirect:/toCourseList";
    }
}
